from django.contrib import admin

# Register your models here.

from .models import TasksInfo
from utils.decorators import log_oper_decorator


@log_oper_decorator
class TasksInfoAdmin(admin.ModelAdmin):
    """
    celery task
    任务基本信息管理
    """
    model = TasksInfo
    date_hierarchy = 'date_done'
    list_display = (
    'task_id', 'colored_status', 'task_name', 'operate_file', 'hostname', 'colored_is_deleted', 'date_done')
    list_filter = ('status', 'date_done', 'task_name', 'hostname')
    readonly_fields = ('date_done', 'result', 'hostname', 'c_time', 'm_time')
    search_fields = ('task_name', 'task_id', 'status', 'traceback', 'hostname')
    fieldsets = (
        ("任务基本信息", {
            'fields': (
                'task_id',
                'task_name',
                'operate_file',
                'status',
                'hostname',
            ),
            'classes': ('extrapretty', 'wide')
        }),
        ("任务参数", {
            'fields': (
                'task_args',
                'task_kwargs',
            ),
            'classes': ('extrapretty', 'wide')
        }),
        ("任务结果", {
            'fields': (
                'result',
                'date_done',
                'traceback',
                'is_deleted',
            ),
            'classes': ('extrapretty', 'wide')
        }),
        ("记录时间", {
            'fields': (
                'c_time',
                'm_time',
            ),
            'classes': ('extrapretty', 'wide')
        }),
    )

    def delete_queryset(self, request, queryset):
        # queryset.delete()
        queryset.update(is_deleted=1)

    def delete_model(self, request, obj):
        """
        Given a model instance delete it from the database.
        """
        # obj.delete()
        obj.is_deleted = 1
        obj.save()


admin.site.register(TasksInfo, TasksInfoAdmin)
